package com.soulkun.smarthome.ui.main.home.dialog

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.AdapterView
import android.widget.BaseAdapter
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.viewModels
import com.soulkun.smarthome.R
import com.soulkun.smarthome.databinding.AppDialogMainHomeDeviceCommandBinding
import com.soulkun.smarthome.databinding.AppSpinnerItemMainHomeDeviceCommandListBinding
import com.soulkun.smarthome.logic.dto.CommandDTO
import com.soulkun.smarthome.ui.main.home.viewmodel.AppMainHomeDeviceCommandViewModel
import com.soulkun.smarthome.ui.main.home.viewmodel.AppMainHomeDeviceViewModel
import soulkun.library.mvvm.component.BaseDataBindingDialogFragment
import soulkun.library.utils.time.TimeUtils
import soulkun.library.utils.toPx
import soulkun.library.utils.toast

class AppMainHomeDeviceCommandDialog(private val deviceViewModel: AppMainHomeDeviceViewModel) :
    BaseDataBindingDialogFragment<AppDialogMainHomeDeviceCommandBinding>() {

    private val viewModel by viewModels<AppMainHomeDeviceCommandViewModel> { defaultViewModelProviderFactory }

    private val timeDialog by lazy {
        AppMainHomeDeviceCommandTimeDialog(viewModel)
    }

    override fun setLayout(): Int {
        return R.layout.app_dialog_main_home_device_command
    }

    override fun doOnViewCreated() {
        requireDialog().window?.run {
            setLayout(requireContext().resources.getDimensionPixelSize(R.dimen.sw_300dp), ViewGroup.LayoutParams.WRAP_CONTENT)
        }
        isCancelable = false
        requireDataBinding().onClickListener = View.OnClickListener {
            when (it.id) {
                R.id.tv_main_home_device_command_text_cancel -> {
                    dismiss()
                }
                R.id.tv_main_home_device_command_text_determine -> {
                    if (viewModel.selectCommandLiveData.value != null && viewModel.selectQosLiveData.value != null && viewModel.selectTimeOutLiveData.value != null) {
                        deviceViewModel.postDeviceCommand(
                            viewModel.selectCommandLiveData.value!!.id,
                            viewModel.selectQosLiveData.value!!,
                            viewModel.selectTimeOutLiveData.value!!
                        ).observe(viewLifecycleOwner) { result ->
                            result.getOrNull()?.let { response ->
                                response.statusMessage.toast()
                                dismiss()
                            }
                        }
                    } else {
                        "请完善指令信息".toast()
                    }
                }
                R.id.tv_main_home_device_command_picker_timeout, R.id.tv_main_home_device_command_icon_timeout_enter -> {
                    timeDialog.show(parentFragmentManager, "CommandTimeDialog")
                }
            }
        }
        deviceViewModel.getDeviceCommandList().observe(viewLifecycleOwner) { result ->
            result.getOrNull()?.let { response ->
                requireDataBinding().spMainHomeDeviceCommandListCommand.run {
                    adapter =
                        object : BaseAdapter() {
                            override fun getCount(): Int {
                                return response.data.size
                            }

                            override fun getItem(position: Int): Any {
                                return response.data[position]
                            }

                            override fun getItemId(position: Int): Long {
                                return position.toLong()
                            }

                            override fun getView(
                                position: Int,
                                convertView: View?,
                                parent: ViewGroup?
                            ): View {
                                val mBinding =
                                    DataBindingUtil.inflate<AppSpinnerItemMainHomeDeviceCommandListBinding>(
                                        LayoutInflater.from(context),
                                        R.layout.app_spinner_item_main_home_device_command_list,
                                        null,
                                        false
                                    )
                                mBinding.deviceCommandDTO = getItem(position) as CommandDTO
                                return mBinding.root
                            }

                        }
                    onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
                        override fun onItemSelected(
                            parent: AdapterView<*>?,
                            view: View?,
                            position: Int,
                            id: Long
                        ) {
                            viewModel.selectCommandLiveData.value =
                                (selectedItem as CommandDTO)
                        }

                        override fun onNothingSelected(parent: AdapterView<*>?) {
                        }

                    }
                }
                requireDataBinding().rgMainHomeDeviceCommandListQos.setOnCheckedChangeListener { _, checkedId ->
                    when (checkedId) {
                        R.id.rb_main_home_device_command_button_qos_false -> {
                            viewModel.selectQosLiveData.value = 0
                        }
                        R.id.rb_main_home_device_command_button_qos_true -> {
                            viewModel.selectQosLiveData.value = 1
                        }
                    }
                }
            }
        }
        viewModel.selectTimeOutLiveData.observe(viewLifecycleOwner) {
            requireDataBinding().tvMainHomeDeviceCommandPickerTimeout.text =
                TimeUtils.secondToTime(it)
        }
        viewModel.selectQosLiveData.value = 0
        viewModel.selectTimeOutLiveData.value = 0
    }

    override fun doOnResume() {
    }

    override fun doOnDismiss() {
    }

    override fun doOnDestroyView() {
    }

}